package com.turing.usdtpay.base;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * @作者：JAY
 * @创建时间：2019/10/10 11:07
 * @描述：
 **/
public class BaseFragment extends Fragment implements View.OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    public void onBackPressed() {

    }

    /**
     * 设置结果，用于{@linkplain #startActivityForResult(Intent, int)}返回结果
     * @param resultCode 结果码
     */
    protected void setResult(int resultCode) {
        Activity activity = getActivity();
        if(null != activity) {
            activity.setResult(resultCode);
        }
    }

    /**
     * 设置结果，用于{@linkplain #startActivityForResult(Intent, int)}返回结果
     * @param resultCode 结果码
     * @param data 结果数据
     */
    protected void setResult(int resultCode, Intent data) {
        Activity activity = getActivity();
        if(null != activity) {
            activity.setResult(resultCode, data);
        }
    }

    /**
     * 退出<br/><br/>
     */
    protected void exit() {
        Activity activity = getActivity();
        if(activity instanceof BaseActivity) {
            ((BaseActivity) activity).exit();
        }
    }

    /**
     * 是否可以回退到上一级
     * @return 是否可以回退到上一级
     */
    protected boolean canGoBack() {
        Activity activity = getActivity();
        if(activity instanceof BaseActivity) {
            return ((BaseActivity) activity).canGoBack();
        }
        return false;
    }

    /**
     * 返回上一级，如果没有上一级，退出Activity
     */
    protected void back() {
        back(1);
    }

    /**
     * 返回上level个级页面，如果栈中Fragment的数量小于等于level个，退出Activity
     * @param level 返回页面个数
     */
    protected void back(int level) {
        Activity activity = getActivity();
        if(activity instanceof BaseActivity) {
            ((BaseActivity) activity).popBack(level);
        }
    }

    /**
     * 打开新的Fragment（系统默认动画）
     * @param fragment Fragment对象
     */
    protected void openNewFragment(android.support.v4.app.Fragment fragment) {
        Activity activity = getActivity();
        if(activity instanceof BaseActivity) {
            ((BaseActivity) activity).addFragmentToStack(fragment);
        }
    }

    /**
     * 打开新的Fragment(带动画)
     * @param fragment Fragment对象
     * @param enter 新的Fragment出现动画
     * @param exit 旧的Fragment消失动画
     * @param popEnter 新的Fragment消失动画
     * @param popExit 旧的Fragment出现动画
     */
    protected void openNewFragmentWithAnimation(android.support.v4.app.Fragment fragment,
                                                @android.support.annotation.AnimRes int enter,
                                                @android.support.annotation.AnimRes int exit,
                                                @android.support.annotation.AnimRes int popEnter,
                                                @android.support.annotation.AnimRes int popExit) {
        Activity activity = getActivity();
        if(activity instanceof BaseActivity) {
            ((BaseActivity) activity).addFragmentToStackWithAnimation(fragment, enter, exit, popEnter, popExit);
        }
    }

    /**
     * 打开新的Fragment(无动画)
     * @param fragment Fragment对象
     */
    protected void openNewFragmentWithoutAnimation(android.support.v4.app.Fragment fragment) {
        Activity activity = getActivity();
        if(activity instanceof BaseActivity) {
            ((BaseActivity) activity).addFragmentToStackWithAnimation(fragment, 0, 0, 0, 0);
        }
    }

    @Override
    public void onClick(View v) {

    }

    /**
     * 取消异步线程任务
     * @param task
     */
    protected void cancelTask(AsyncTask task) {
        if(null != task && !task.isCancelled()) {
            task.cancel(true);
            task = null;
        }
    }


    /**
     * 显示一个短Toast
     * @param text
     */
    protected void showShortToast(CharSequence text) {
        Activity activity = getActivity();
        if(null != activity) {
            Toast.makeText(activity, text, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 显示一个短Toast
     * @param resId
     */
    protected void showShortToast(int resId) {
        Activity activity = getActivity();
        if(null != activity) {
            Toast.makeText(activity, resId, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 显示一个长Toast
     * @param text
     */
    protected void showLongToast(CharSequence text) {
        Activity activity = getActivity();
        if(null != activity) {
            Toast.makeText(activity, text, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 显示一个长Toast
     * @param resId
     */
    protected void showLongToast(int resId) {
        Activity activity = getActivity();
        if(null != activity) {
            Toast.makeText(activity, resId, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 获取资源id，如果没有找到，返回0
     * @param name
     * @param defType
     * @return
     */
    protected int getIdentifier(String name, String defType) {
        return getResources().getIdentifier(name, defType, getActivity().getPackageName());
    }

    protected int getResourceColor(int resId) {
        return getResources().getColor(resId);
    }

    /**
     * 设置文本不能输入汉字（包括emoji符号等）
     */
    protected void forbidCNInput(EditText editText) {
        InputFilter filter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                //返回null表示此字符可以输入,返回空字符串表示禁止输入此字符
                if (source.toString().length() != source.toString().getBytes().length) {
                    return "";
                } else {
                    return null;
                }
            }
        };
        editText.setFilters(new InputFilter[]{filter});
    }

    /**
     * 设置标题
     * @param title
     */
    protected void setTitle(String title) {
        Activity activity = getActivity();
        if(activity instanceof BaseActivity) {
            ((BaseActivity) activity).setTitle(title);
        }
    }

    protected void showTitleLogo(boolean show){
        Activity activity = getActivity();
        if(activity instanceof BaseActivity) {
            ((BaseActivity) activity).showTitleLogo(show);
        }
    }

    protected void setOnClickTitleLogo(View.OnClickListener listener){
        Activity activity = getActivity();
        if(activity instanceof BaseActivity) {
            ((BaseActivity) activity).setOnClickTitleLogo(listener);
        }
    }

    protected void showBackBtn(boolean show){
        Activity activity = getActivity();
        if(activity instanceof BaseActivity) {
            ((BaseActivity) activity).showBackBtn(show);
        }
    }

    protected void setBackListener(View.OnClickListener listener){
        Activity activity = getActivity();
        if(activity instanceof BaseActivity) {
            ((BaseActivity) activity).setBackListener(listener);
        }
    }

    protected void showProgressBar(boolean show){
        Activity activity = getActivity();
        if(activity instanceof BaseActivity) {
            ((BaseActivity) activity).showProgressBar(show);
        }
    }

    protected void updateOrder(){
        Activity activity = getActivity();
        if(activity instanceof BaseActivity) {
            ((BaseActivity) activity).updateOrder();
        }
    }

    //    /**
//     * 加载图片
//     * @param url 图片地址
//     * @param imageView 图片显示控件
//     * @param width 显示宽度
//     * @param height 显示高度
//     * @param placeHolder 加载中图片
//     * @param error 加载错误图片
//     */
//    protected void loadImage(String url, ImageView imageView, int width, int height, int placeHolder, int error) {
//        if(TextUtils.isEmpty(url)) {
//            return;
//        }
//        Picasso.get()
//                .load(Uri.parse(url))
//                .placeholder(placeHolder)
//                .error(error)
//                .resize(width, height)
//                .into(imageView);
//    }
//
//    /**
//     * 加载图片
//     * @param url 图片地址
//     * @param imageView 图片显示控件
//     * @param width 显示宽度
//     * @param height 显示高度
//     */
//    protected void loadImageCenterCrop(String url, ImageView imageView, int width, int height, Callback callback) {
//        if(TextUtils.isEmpty(url)) {
//            return;
//        }
//        Picasso.get()
//                .load(Uri.parse(url))
//                .resize(width, height)
//                .centerCrop()
//                .into(imageView, callback);
//    }
//
//    /**
//     * 加载图片
//     * @param url 图片地址
//     * @param imageView 图片显示控件
//     */
//    protected void loadImage(String url, ImageView imageView, Callback callback) {
//        if(TextUtils.isEmpty(url)) {
//            return;
//        }
//        Picasso.get()
//                .load(Uri.parse(url))
//                .into(imageView, callback);
//    }
//
//    /**
//     * 将View构建成MaterialRippleLayout风格
//     * @param view 需要添加MaterialRippleLayout
//     * @param listener 点击事件
//     */
//    protected MaterialRippleLayout materialRippleLayout(View view, View.OnClickListener listener) {
//        MaterialRippleLayout layout = MaterialRippleLayout.on(view)
//                .rippleOverlay(true)
//                .rippleHover(true)
//                .rippleAlpha(0.2f)
//                .create();
//        layout.setClickable(true);
//        layout.setOnClickListener(listener);
//        return layout;
//    }
}
